package com.zcah.weixinspider.controller;

import com.zcah.weixinspider.service.BloomFilter;
import com.zcah.weixinspider.service.IRedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class WechatController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private IRedisService redisService;
    @Autowired
    private BloomFilter filter;

    @RequestMapping(value = "wechat/postlist", method = RequestMethod.POST)
    public long wechatList(@RequestBody HashMap<String, Object> reqMap) {
        List<String> url_list = (List<String>) reqMap.get("url_list");
        for (String url : url_list) {
            if (!filter.addIfNotExist(url)) {
                redisService.put("url_list", url);
                logger.info("insert one url");
            }
        }
        return url_list.size();
    }

    @RequestMapping(value = "wechat/savebloom", method = RequestMethod.GET)
    public void saveBloom() {
        filter.saveFilterToFile("./bloom.obj");
    }

    @RequestMapping(value = "test", method = RequestMethod.GET)
    public Boolean test() {
        return Boolean.TRUE;
    }
}
